<!DOCTYPE html>
<html lang="zh-CN">
<!-- 登录页面：用户输入用户名和密码进行身份验证 -->

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <!-- 引入公共样式和登录页面专用样式 -->
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
    <!-- 顶部导航栏 -->
    <div class="nav">
        <img src="pic/logo2.jpg" alt="博客系统Logo">
        <span class="blogInfo-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <!-- 登录表单容器 -->
    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <!-- 用户名输入框 -->
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username" placeholder="请输入用户名">
            </div>
            <!-- 密码输入框 -->
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password" placeholder="请输入密码">
            </div>
            <!-- 提交按钮 -->
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>
        </div>
    </div>

    <!-- 引入jQuery库 -->
    <script src="js/jquery.min.js"></script>
    <script>
        /**
         * 用户登录功能
         * 通过Ajax向后端发送登录请求，验证用户身份
         */
        function login() {
            $.ajax({
                type: "post",
                url: "/userInfo/login",
                contentType: "application/json",
                data: JSON.stringify({
                    userName: $("#username").val(),
                    password: $("#password").val()
                }),
                success: function (result) {
                    if (result.code == 200 && result.data != null) {
                        // 用户登录成功，保存用户ID和token到localStorage
                        localStorage.setItem("loginUserId", result.data.userId);
                        localStorage.setItem("token", result.data.token);
                        // 跳转到博客列表页面
                        location.href = "blog_list.html";
                    } else {
                        // 登录失败，显示错误信息
                        alert(result.errMsg);
                    }
                }
            });
        }
    </script>
</body>

</html>